package com.zx.sp_mybatisplus_smbms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.sp_mybatisplus_smbms.pojo.SmbmsBill;
import com.zx.sp_mybatisplus_smbms.pojo.SmbmsProvider;
import com.zx.sp_mybatisplus_smbms.service.SmbmsBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.Wrapper;
import java.util.List;

@Controller
@RequestMapping("/bill/")
public class BillControler {
    @Autowired
    private SmbmsBillService billService;

//    @RequestMapping("list")
//    public String list(Model model){
//        List<SmbmsBill> blist = billService.list();
//        model.addAttribute("blist",blist);
//        return "billlist";
//    }

    @RequestMapping("view")
    public String view(Integer id,Model model){
        SmbmsBill bill = billService.getById(id);
        model.addAttribute("bill",bill);
        return "billview";
    }

    //删除操作
    @RequestMapping("del")
    public String del(Long id, Model model) {
        boolean flag = billService.removeById(id);
        if (flag) {
            return "redirect:list";
        } else {
            model.addAttribute("msg", " 删除失败");
            return "billlist";
        }
    }

    @RequestMapping("goadd")
    public String goadd(){
        return "billadd";
    }

    @RequestMapping("doadd")
    public String doadd(Model model,SmbmsBill bill){
        boolean flag = billService.save(bill);
        if (flag){
            return "forward:list";
        }else {
            return "billadd";
        }
    }
    //修改前查询
    @RequestMapping("goupdate")
    public String goupdate(Integer id, Model model) {
        SmbmsBill bill = billService.getById(id);
        model.addAttribute("bill", bill);
        return "billmodify";
    }

    //修改操作
    @RequestMapping("doupdate")
    public String doupdate(SmbmsBill bill, Model model) {
        boolean flag = billService.updateById(bill);
        if (flag) { //修改成功  再次查詢下
            return "redirect:list";
//            return "forward:list";
        } else {  //修改失败
            model.addAttribute("msg", "修改失败！");
            return "billmodify";
        }
    }

    @RequestMapping("list")
    public String listpages(Integer current, Model model,String queryProductName,Integer queryProviderId,Integer queryIsPayment){
        current=current==null?1:current;
        Page<SmbmsBill> page=new Page<SmbmsBill>(current,3);
        QueryWrapper<SmbmsBill> wrapper = new QueryWrapper<SmbmsBill>();
        if (queryProductName!=null&&queryProductName!=""){
            wrapper.like("productname",queryProductName);
        }
        if(queryIsPayment!=null&&queryIsPayment!=0){
            wrapper.like("ispayment",queryIsPayment);
        }
        if(queryProviderId!=null&&queryProviderId!=0){
            wrapper.like("providerid",queryProviderId);
        }
        Page<SmbmsBill> page1 = billService.page(page);
        model.addAttribute("bpage",page1);
        model.addAttribute("queryProductName",queryProductName);
        model.addAttribute("queryProviderId",queryProviderId);
        model.addAttribute("queryIsPayment",queryIsPayment);


        return "billlist";
    }

//    @RequestMapping("find")
//    public String find(String productname,Long providerid, Integer ispayment, Model model) {
//        List<SmbmsBill> billList = billService.findBillListByNameAndIspaymentProviderid(productname,providerid,ispayment);
//        model.addAttribute("billList", billList);
//        model.addAttribute("productname", productname);
//        model.addAttribute("providerid",providerid);
//        model.addAttribute("ispayment", ispayment);
//        return "billlist";
//    }

}
